package cn.easytogo.action.appuser;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.User;
import cn.easytogo.service.IResourceDAO;
import cn.easytogo.service.IUserDAO;
import cn.easytogo.util.ConfigUtil;
import cn.easytogo.util.CookieUtil;

public class AppUserAction extends BaseAction{
	private IUserDAO userDAO;
	private IResourceDAO resourceDAO;
	private String username;
	private String password;
	private String email;
	private String realname;
	private String icode;
	private String msg1;
	private String msg2;
	private String msg3;
	private String admin;//管理员登陆标志位
	private String remember;//记住用户名和密码标志位
	/**
	 * 用户名验证
	 * */
	public void usernameExist(){
		if(username==null||"".equals(username)){
			msg1 = "用户名不能空";
		}else{
			try{
			User userExist = userDAO.findUserByUserName(username);
				if(userExist!=null){
					msg1 = "用户名已存在";
				}else{
					msg1="success";
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		print(msg1);
	}
	/**
	 * 邮箱验证
	 * */
	public void emailExist(){
		if(email==null||"".equals(email)){
			msg2 = "邮箱不能空";
		}else{
			try{
				User emailExist = userDAO.findUserByEmail(email);
				if(emailExist!=null){
					msg2 = "邮箱已注册";
				}else{
					msg2="success";
				}
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		print(msg2);
	}
	/**
	 * 验证码验证
	 * */
	public void icodeVerify(){
		if(icode==null||"".equals(icode)){
			msg3 = "验证码不能空";
		}else{
			String rightCode = (String) session.get("icode");
			if(icode==null||!icode.equalsIgnoreCase(rightCode)){
				msg3 = "验证码不正确";
			}else{
				msg3="success";
			}
		}
		print(msg3);
	}
	/**
	 * 注册
	 * @throws SQLException 
	 * */
	public String regist(){
		boolean failed = false;
		String rightCode = (String) session.get("icode");
		if(username==null||"".equals(username)){
			msg1 = "用户名不能空";
			failed = failed||true;
		}
		if(email==null||"".equals(email)){
			msg2 = "邮箱不能空";
			failed = failed||true;
		}
		if(icode==null||!icode.equalsIgnoreCase(rightCode)){
			msg3 = "验证码不正确";
			failed = failed||true;
		}
		if(failed){
			return "regist_failed";
		}
		try{
			User userExist = userDAO.findUserByUserName(username);
			if(userExist!=null){
				msg1 = "用户名已存在";
				return "regist_failed";
			}
			User emailExist = userDAO.findUserByEmail(email);
			if(emailExist!=null){
				msg2 = "邮箱已注册";
				return "regist_failed";
			}
			User userToInsert = new User();
			userToInsert.setUsername(username);
			userToInsert.setPassword(password);
			userToInsert.setEmail(email);
			userToInsert.setRealname(realname);
			userToInsert.setUsergroup_id(getDefault_usergroup_id());
			userDAO.insertUser(userToInsert);
			msg1 = "注册成功!";
		}catch (SQLException e) {
			return "error";
		}
		return "regist_success";
	}
	
	/**
	 * 登录
	 * */
	public String login(){
		try {
			User logUser = userDAO.findUserByUserNameAndPassword(username, password);
			if(logUser==null){
				msg1 = "用户名或密码错误!";
				return "login_failed";
			}
			if(admin!=null&&"1".equals(admin)&&!"1".equals(logUser.getAdmin())){
				msg1 = "此用户不是管理员用户!";
				return "login_failed";
			}
			logUser.setResources(resourceDAO.selectResourcesByUser(logUser));
			session.put(ConfigUtil.getConfigByName("session_name"), logUser);
		} catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		
		try{//检查是否需要记住账号
			if("1".equals(remember)){
				CookieUtil.saveCookie("username", username, response);
				CookieUtil.saveCookie("password", password, response);
				CookieUtil.saveCookie("admin", admin, response);
			}else{//如果不记住,把cookie清空
				CookieUtil.delete("username", response);
				CookieUtil.delete("password", response);
				CookieUtil.delete("admin", response);
			}
		}catch(UnsupportedEncodingException e){
			e.printStackTrace();
		}
		if(admin!=null&&"1".equals(admin)){
			return "login_admin_success";
		}
		return "login_success";
	}
	
	/**
	 * 前往登陆页面
	 * */
	public String toLogin(){
		try {
			username = CookieUtil.getCookie("username", request);
			if(username!=null&&!"".equals(username)){//如果用户名不为空,说明用户记住了配置,再读取密码
				password = CookieUtil.getCookie("password", request);
				admin = CookieUtil.getCookie("admin", request);
				remember="1";
			}else{
				username="";
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_login";
	}
	
	/**
	 * 注销
	 * */
	public String logout(){
		session.remove(ConfigUtil.getConfigByName("session_name"));
		return "logout_success";
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getIcode() {
		return icode;
	}
	public void setIcode(String icode) {
		this.icode = icode;
	}

	public String getMsg1() {
		return msg1;
	}

	public void setMsg1(String msg1) {
		this.msg1 = msg1;
	}

	public String getMsg2() {
		return msg2;
	}

	public void setMsg2(String msg2) {
		this.msg2 = msg2;
	}

	public String getMsg3() {
		return msg3;
	}

	public void setMsg3(String msg3) {
		this.msg3 = msg3;
	}

	public String getAdmin() {
		return admin;
	}

	public void setAdmin(String admin) {
		this.admin = admin;
	}

	public String getRemember() {
		return remember;
	}

	public void setRemember(String remember) {
		this.remember = remember;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public IResourceDAO getResourceDAO() {
		return resourceDAO;
	}
	public void setResourceDAO(IResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}
	
}
